package dtu.app.listloader;

import java.util.Collections;
import java.util.List;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.content.AsyncTaskLoader;
import android.util.Log;
import dtu.app.db.ProductDatabaseHelper;
import dtu.app.model.Product;

public class ProductListLoader extends AsyncTaskLoader<List<Product>> {
    
    List<Product> mProducts;
    Bundle args;
    
    public ProductListLoader(Context context, Bundle b) {
    	super(context);
    	this.args = b;
    }

    @Override
    public List<Product> loadInBackground() {
        System.out.println("DataListLoader.loadInBackground");
         // You should perform the heavy task of getting data from 
         // Internet or database or other source 
        
        String category = "DAIRY"; // show dairy at startup
        
        if (args != null) {
            category = args.getString("category");
            Log.i("CATEGORY", category);
        }
        
        // Create corresponding array of products and load with data from the database.
        ProductDatabaseHelper db = new ProductDatabaseHelper(getContext());
        List<Product> products = db.getProductsByCategory(category);
        Collections.sort(products, Product.ProductNameComparator); // sort products by their name
        
        return products;
    }
     
    /**
     * Called when there is new data to deliver to the client.  The
     * super class will take care of delivering it; the implementation
     * here just adds a little more logic.
     */
    @Override public void deliverResult(List<Product> listOfData) {
        if (isReset()) {
            // An async query came in while the loader is stopped.  We
            // don't need the result.
            if (listOfData != null) {
                onReleaseResources(listOfData);
            }
        }
        List<Product> oldApps = listOfData;
        mProducts = listOfData;

        if (isStarted()) {
            // If the Loader is currently started, we can immediately
            // deliver its results.
            super.deliverResult(listOfData);
        }

        // At this point we can release the resources associated with
        // 'oldApps' if needed; now that the new result is delivered we
        // know that it is no longer in use.
        if (oldApps != null) {
            onReleaseResources(oldApps);
        }
    }

    /**
     * Handles a request to start the Loader.
     */
    @Override protected void onStartLoading() {
        if (mProducts != null) {
            // If we currently have a result available, deliver it
            // immediately.
            deliverResult(mProducts);
        }


        if (takeContentChanged() || mProducts == null) {
            // If the data has changed since the last time it was loaded
            // or is not currently available, start a load.
            forceLoad();
        }
    }

    /**
     * Handles a request to stop the Loader.
     */
    @Override protected void onStopLoading() {
        // Attempt to cancel the current load task if possible.
        cancelLoad();
    }

    /**
     * Handles a request to cancel a load.
     */
    @Override public void onCanceled(List<Product> apps) {
        super.onCanceled(apps);

        // At this point we can release the resources associated with 'apps'
        // if needed.
        onReleaseResources(apps);
    }

    /**
     * Handles a request to completely reset the Loader.
     */
    @Override protected void onReset() {
        super.onReset();

        // Ensure the loader is stopped
        onStopLoading();

        // At this point we can release the resources associated with 'apps'
        // if needed.
        if (mProducts != null) {
            onReleaseResources(mProducts);
            mProducts = null;
        }
    }

    /**
     * Helper function to take care of releasing resources associated
     * with an actively loaded data set.
     */
    protected void onReleaseResources(List<Product> apps) {}
         
}